<template>
  <div>
    <div class="container">
      <div class="handle-box">

        <el-row>
          <el-col :xs="6" :sm="6" :md="6" :lg="6" :xl="6">
            <el-button type="primary" icon="el-icon-plus" class="handle-del mr10 " @click="handleAdd">添加
            </el-button>
          </el-col >
          <el-col :xs="6" :sm="6" :md="6" :lg="6" :xl="6">
            <el-button type="primary" icon="el-icon-delete" class="handle-del mr10 deleteButtons" @click="delAllSelection">批量删除
            </el-button>
          </el-col>
       <!--   <el-col :xs="6" :sm="6" :md="6" :lg="6" :xl="6">
            <el-input v-model="keyWord"  placeholder="请输入管理员编号" class="handle-input mr10 searchLabel"/>
          </el-col>
          <el-col :xs="6" :sm="6" :md="6" :lg="6" :xl="6">
            <el-button class="searchButton" type="primary"icon="el-icon-search" @click="handleSearch(keyWord)">搜索</el-button>
          </el-col>-->
        </el-row>

      </div>
      <el-table :data="tableData"  border class="table" ref="multipleTable" header-cell-class-name="table-header"
                @selection-change="handleSelectionChange">
        <el-table-column  type="selection" align="center"/>
        <el-table-column type="index" :index="indexMethod" label="管理员编号" width="100" align="center"/>
        <el-table-column prop="account" label="账号" min-width="100" align="center"/>
        <el-table-column label="权限" min-width="100" align="center">
          <template slot-scope="scope">
            {{format(scope.row)}}
          </template>
        </el-table-column>

        <el-table-column label="操作" min-width="180" align="center" fixed="right">
          <template slot-scope="scope">
            <!--<el-button type="text" class="editButton" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑
            </el-button>-->
            <el-button type="text" class="editButton" icon="el-icon-edit" @click="handleReset(scope.$index, scope.row)">重置密码
            </el-button>
            <el-button type="text"  icon="el-icon-delete" class="red deleteButton" @click="handleDelete(scope.$index, scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination background layout="total, prev, pager, next" :current-page="query.currentPage"
                       :page-size="query.pageSize" :total="query.total" @current-change="handlePageChange"/>
      </div>
    </div>

    <!-- 编辑弹出框 -->
 <!--   <el-dialog title="编辑管理员信息" :visible.sync="editVisible" width="35%" >
      <el-form :model="form" :label-width="formLabelWidth">

        <el-row>
          <el-form-item label="管理员账号" prop="account">
            <el-input disabled prefix-icon="el-icon-search" placeholder="请输入管理员账号" v-model="form.account" autocomplete="off" ></el-input>
          </el-form-item>
        </el-row>

        <el-row>
          <el-form-item label="管理员名称" prop="name">
            <el-input  prefix-icon="el-icon-search" placeholder="请输入管理员名称" v-model="form.name" autocomplete="off" ></el-input>
          </el-form-item>
        </el-row>

        <el-row>
          <el-form-item label="管理员密码" prop="password">
            <el-input  prefix-icon="el-icon-search" placeholder="请输入管理员密码" v-model="form.password" autocomplete="off" ></el-input>
          </el-form-item>
        </el-row>

        <el-row>
          <el-form-item label="管理员权限" prop="power">
            <el-input  prefix-icon="el-icon-search" placeholder="请输入管理员名称" v-model="form.power" autocomplete="off" ></el-input>
          </el-form-item>
        </el-row>


        <el-form-item>
          <el-button class="submitButton"  type="primary" @click="edit">编辑</el-button>
          <el-button class="cancelButton" @click="cancel" >取消</el-button>
        </el-form-item>


      </el-form>
    </el-dialog>-->

    <!-- 添加弹出框 -->
    <el-dialog title="添加管理员信息" :visible.sync="addVisible" width="35%" >
      <el-form :model="form" :label-width="formLabelWidth">
        <el-row>
            <el-form-item label="管理员账号" prop="account">
              <el-input  prefix-icon="el-icon-search" placeholder="请输入管理员账号" v-model="form.account" autocomplete="off" ></el-input>
            </el-form-item>
        </el-row>


        <el-row>
          <el-form-item label="管理员密码" prop="password">
            <el-input  prefix-icon="el-icon-search" placeholder="请输入管理员密码" v-model="form.password" autocomplete="off" ></el-input>
          </el-form-item>
        </el-row>

        <el-form-item>
          <el-button class="submitButton"  type="primary" @click="submit">添加</el-button>
          <el-button class="cancelButton" @click="cancel" >取消</el-button>
        </el-form-item>


      </el-form>
    </el-dialog>

    <!-- 重置密码框 -->
    <el-dialog title="重置管理员密码" :visible.sync="resetVisible" width="30%">
      <el-form :model="form" :label-width="formLabelWidth">
        <el-row>
          <el-form-item label="账号" prop="account">
            <el-input disabled  prefix-icon="el-icon-search" placeholder="请输入学号/工号" v-model="form.account" autocomplete="off" ></el-input>
          </el-form-item>
        </el-row>

        <el-row>
          <el-form-item  label="新密码">
            <el-input v-model="newPassword" prefix-icon="el-icon-search" placeholder="请输入重置后的密码" autocomplete="off"></el-input>
          </el-form-item>
        </el-row>


        <el-form-item>
          <el-button class="submitButton"  type="primary" @click="reset">修改</el-button>
          <el-button class="cancelButton" @click="cancel" >取消</el-button>
        </el-form-item>


      </el-form>
    </el-dialog>
  </div>
</template>

<script>
  import manager from '../../utils/manager'
  import { queryAdmins, updatePassword, deleteAdmin, deleteAdmins, addAdmin } from '../../api/index';
  export default {
    name: 'ManagerManage',
    data() {
      return {
        keyWord: '',
        /* 分页 */
        query: {
          total: 10,
          pageSize: 7,
          currentPage: 1,
        },
        tableData: [],
        multipleSelection: [],
        delList: [],
        editVisible: false,
        addVisible: false,
        resetVisible: false,
        pageTotal: 0,
        newPassword: '',
        form: {
          adminId: '',
          account: '',
          name: '',
          password: '',
          power: '',
        },
        idx: -1,
        id: -1,
        formLabelWidth: '30%',
        list: [],
        /* 校验规则 */
      }
    },
    created() {
      this.getData();
    },
    methods: {
      // 获取 easy-mock 的模拟数据
      async getData() {

        let current = this.query.currentPage;
        let size = this.query.pageSize;

        const data = await queryAdmins({current:current,size:size});
        //console.log(data.records);
        console.log(data);

        this.tableData = data.records;

        this.query.total = data.total;
        for (let i = 0; i < data.records.length; i++) {
          let id = i + 1;
          let adminId = data.records[i].adminId;
          let ids = {
            id: id,
            collegeId: adminId
          };
          this.list.push(ids);
        }
      },


      // 触发添加页面
      handleAdd(){
        this.addVisible = true;
      },

      indexMethod(index) {
        return (this.query.currentPage - 1) * this.query.pageSize + index + 1;
      },

      format(row){
        return  row.power === '1' ? '超级管理员':'普通管理员';
      },


      // 触发搜索按钮
      handleSearch(keyWord) {
        // 发送查询请求,查询内容为keyWord
        // 获取返回数据然后渲染表格


        //this.getData();
      },
      // 删除操作
      async handleDelete(index, row) {

        // 将当前用户id发送给后台
        console.log(row.adminId);

        let adminId = row.adminId;
        let account = row.account;
        console.log(account);
        let myCount = localStorage.getItem('account');
        console.log(myCount);
        if (account === 'admin' || myCount === account){
          this.$message.error("无法删除!");
        }else{
          await deleteAdmin({ adminId });
          this.$message.success("删除成功!");
        }
        this.getData();

      },

      // 多选操作
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
      // 多删
      async delAllSelection() {
        // 获得多选删除的id数组
        const length = this.multipleSelection.length;
        console.log(length);
        if (length === 0) {
          this.$message.error(`请选择要删除的对象`);
        } else {
          this.delList = this.delList.concat(this.multipleSelection);


          let list = [];
          for (let i = 0; i < length; i++) {
            list.push(this.multipleSelection[i]);
          }
          console.log(list);


          await deleteAdmins({ list:list });

          this.multipleSelection = [];
          this.getData();
        }

      },
      // 编辑操作
      handleEdit(index, row) {
        this.idx = index;
        this.form = row;
        this.editVisible = true;
      },

      // 重置密码
      async handleReset(index, row) {
        this.idx = index;
        this.form = row;
        this.resetVisible = true;
        console.log(row);

      },

      // 分页导航
      async handlePageChange(val) {
        /* 更换当前页 */

        let size = this.query.pageSize;
        const data = await queryAdmins({ current: val, size:size });
        this.tableData = data.records;
        this.query.total = data.total;
        console.log(this.tableData);

        this.list = [];
        for (let i = 0; i < data.records.length; i++) {
          let id = i + 1;
          let adminId = data.records[i].adminId;
          let ids = {
            id: id,
            collegeId: adminId
          };
          this.list.push(ids);
        }


      },

      // 提交表单
      async submit() {
        //数据校验
        console.log(this.form);
        //提交表单数据

        let account = this.form.account;
        let password = this.form.password;

        await addAdmin({ account, password });

        this.addVisible = false;

        // 页面重新渲染
        this.getData();

      },

      // 编辑表单
      edit(){

        //数据校验

        //提交表单数据

        this.editSuccess();
        this.editVisible = false;

        // 页面重新渲染
      },

      // 管理员重置密码
      async reset() {

        let adminId = this.form.adminId;
        let account = this.form.account;
        console.log(account);
        let password = this.newPassword;
        console.log(password);

        const data = await updatePassword({adminId,account,password });

        this.$message.success("重置密码成功!");
        this.resetVisible = false;

      },

      // 取消表单
      cancel(){
        this.form = {};
        this.newPassword = '';
        this.addVisible = false;
        this.editVisible = false;
        this.resetVisible = false;
      },


      //提交成功!
      submitSuccess() {
        this.$notify({
          title: '成功',
          message: '添加用户成功!',
          type: 'success'
        });
      },
      editSuccess() {
        this.$notify({
          title: '成功',
          message: '编辑用户成功!',
          type: 'success'
        });
      },
    },
    watch:{
      resetVisible(){
        if(this.resetVisible === false){
          this.newPassword = '';
        }
      },
      addVisible(){
        if(this.addVisible === false){
          this.form = {};
        }
      },
      editVisible(){
        if(this.editVisible === false){
          this.form = {};
        }
      }
    }
  };
</script>

<style scoped>
  .handle-box {
    margin-bottom: 20px;
  }


  .handle-input {
    width: 300px;
    display: inline-block;
  }

  .table {
    width: 100%;
    font-size: 14px;
  }

  .red {
    color: #ff0000;
  }

  .mr10 {
    margin-right: 10px;
  }


  /* 提交按钮 */
  .submitButton{
    margin-left: 20%;
  }

  /* 取消按钮 */
  .cancelButton{
    margin-left: 20%;
  }

  /* 按钮大小 */
  .editButton,.deleteButton{
    font-size: 14px;
  }

  /* 删除 搜索 按钮 */
  .deleteButtons{
    margin-left: -30%;
  }

  .searchLabel{
    margin-left: 20%;
  }

  .searchButton{
    margin-left: 25%;
  }


</style>
